//
// Created by Administrator on 2023/11/21.
//
#include "iostream"
#include "algorithm"
#include "cmath"

using namespace std;

const int N = 1e5 + 10;
int a[N];

int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    sort(a, a + n);

    int mid = a[n >> 1];//取中间点

    long long res = 0;
    for (int i = 0; i < n; ++i) {
        res += abs(mid - a[i]);
    }
    cout << res;
}